<?php


class HaulApp extends BackendApp
{
	var $_haul_mod;


	var $_haul_collect_mod;

	function __construct()
	{
		$this->HaulApp();
	}

	function HaulApp()
	{
		parent::BackendApp();
		$this->_haul_mod =& m('haul');
		$this->_haul_collect_mod =& m('haul_collect');
	}

	function index()
	{
		$stauts=$_GET['stauts'];



		$db=&db();
		$sql="SELECT d_key,d_value from zys_dictionary WHERE d_code='haulstatus'";
		$types=array();
		$temp=$db->getAll($sql);
		foreach ($temp as $v){
			$types[$v['d_key']]=$v['d_value'];
		}
		$this->assign('status', $types);


		$wm_carrier=empty($_GET['h_name'])?'':$_GET['h_name'];

		$conditions .= "1=1 AND (h_short_name LIKE '%".$wm_carrier."%' or h_name like '%".$wm_carrier."%')";
		if ($_GET['id']!=null&&$_GET['id']!=''){
			$wid=$_GET['id'];
			$conditions .=" and h_wmtid=".$wid;
			$this->assign('wid', $wid);
		}else if($_GET['wid']!=null&&$_GET['wid']!=''&&$_GET['wid']!=0){
			$conditions .=" and h_wmtid=".$_GET['wid'];
			$this->assign('wid', $_GET['wid']);
		}

		if($_GET['type_id']!=''){
			$conditions.=" and ifnull(h_finished,0)=".intval($_GET['type_id']);
		}

		$page   =   $this->_get_page(10);   //获取分页信息



		$haul=$this->_haul_mod->find(array(
	        'fields'   => 'this.*', 
		    'conditions'  => $conditions,
	        'limit'   => $page['limit'],
        	'count'   => true ,  //允许统计,
			'order'=>'h_wmtid DESC,h_order ASC'
			));    //找出所有的运单
			$page['item_count']=$this->_haul_mod->getCount();   //获取统计数据

			$this->assign('stauts', $stauts);
			$this->_format_page($page);
			$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
			$this->assign('haul', $haul);
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

			$this->assign('tabself', json_encode(array('text'=>'运程管理')));

			$this->display('haul.index.html');
	}




	//新增
	function add()
	{

		$id=$_GET['id'];
		if (!IS_POST)
		{

			$this->assign('editpage', false);

			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));



			$db=&db();
			$sql="SELECT d_key,d_value FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);

			$this->assign('tabself', json_encode(array('text'=>'新建运程')));
			$this->assign('tabback', json_encode(array('text'=>'编辑运单','url'=>'')));

			$this->display('haul.form.html');

		}else {

			//h_finished




			$data = array(
				'h_wmtid'=>$id,
                'h_name'  => $_POST['h_name'],
			//  'h_required'  => $_POST['h_required'],
                'h_order' => $_POST['h_order'],
                'h_address'	     => $_POST['h_address'],
			    'h_linkman'      => $_POST['h_linkman'],

			  'h_linkman_tel'	     => $_POST['h_linkman_tel'],
			    'h_note'      => $_POST['h_note'],
				 'h_required_service_date'      => $_POST['h_required_service_date'],



			    'h_finished_date'      => strtotime($_POST['h_finished_date']),

			);

			if ($_POST['h_finished']=='异常'){
				$data['h_finished']=-1;
			}elseif ($_POST['h_finished']=='未开始')
			{
				$data['h_finished']=0;
			}elseif ($_POST['h_finished']=='完成')
			{
				$data['h_finished']=2;
			};

			/* 保存 */
			$rows = $this->_haul_mod->add($data);
			if ($this->_haul_mod->has_error())
			{
				$this->show_warning($this->_haul_mod->get_error());
				return;
			}

			$this->show_message('add_ok',
                'back_list',    'index.php?app=haul',
                'edit_again',   "index.php?app=haul&amp;act=add&amp;id=.$id."
			);


		}

	}





	//添加运程定义采集信息
	function add_haul_define_collect()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);



		if (!IS_POST)
		{
			$db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);




			$this->assign('tabself', json_encode(array('text'=>'添加采集信息')));
			$this->assign('tabback', json_encode(array('text'=>'采集信息','url'=>'')));


			$this->import_resource('jquery.plugins/jquery.validate.js');
			$this->display('haul_collect.form.html');
		}
		else
		{
			$data = array(

				'h_collect_type' => $_POST['h_collect_type'],
                'h_collect_des' => $_POST['h_collect_des'],
                'hc_h_tid'  => $id,

			);





			if (!$id = $this->_haul_collect_mod->add($data))
			{
				$this->show_warning('添加失败');
				return;
			}
			$this->show_message('添加成功',
                'back_list',    'index.php?app=haul',
                'continue_add', 'index.php?app=haul_define&amp;act=add_haul_define_collect'
                );
		}
	}




	//编辑运单定义采集信息
	function edit_haul_define_collect()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);


			

		if (!IS_POST)
		{

			$haul_define=$this->_haul_collect_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_tid='.$id,
			));
			$db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);





			$haul_define_collect=$this->_haul_collect_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_h_tid='.$id,
			));


			$this->assign('haul_collect', $haul_define_collect);



			$this->assign('haul_define', $haul_define);

			$this->assign('tabself', json_encode(array('text'=>'编辑采集信息')));
			$this->assign('tabback', json_encode(array('text'=>'采集信息','url'=>'')));


			$this->display('haul_define_collect.form.html');
		}else {

			$data = array(
				'h_collect_type' => $_POST['h_collect_type'],
                'h_collect_des' => $_POST['h_collect_des'],

			);




			/* 保存 */
			$rows = $this->_haul_collect_mod->edit($id, $data);
			if ($this->_haul_collect_mod->has_error())
			{
				$this->show_warning($this->_haul_collect_mod->get_error());
				return;
			}

			$this->show_message('编辑成功',
                'back_list',    'index.php?app=haul',
                '返回编辑',   'index.php?app=haul_define&amp;act=edit_haul_define_collect&amp;id=' . $id
			);


		}


	}









	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		$stauts = empty($_GET['stauts']) ? 0 : $_GET['stauts'];


		if (!IS_POST)
		{
			$db=&db();
			$sql="SELECT d_key,d_value FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);

			$this->assign('editpage', true);
			$haul=$this->_haul_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'h_tid='.$id,
			));


			$this->assign('id', $id);




			$haul_define_collect=$this->_haul_collect_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_h_tid='.$id,
			));


			$this->assign('haul_define_collect', $haul_define_collect);


			if ($stauts=='新建'||$stauts=='已接单'){
				$this->assign('stauts', 1);
			}




			$this->assign('haul', $haul);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

			$this->assign('tabself', json_encode(array('text'=>'编辑运程')));
			$this->assign('tabback', json_encode(array('text'=>'运程管理','url'=>'')));

			$this->display('haul.form.html');
		}else
		{



			$data = array(
                'h_short_name'  => $_POST['h_short_name'],
				'h_type'  => $_POST['field'],
			 	'h_name'  => $_POST['h_name'],
			//  'h_required'  => $_POST['h_required'],
                'h_order' => $_POST['h_order'],
                'h_address'	     => $_POST['h_address'],
			    'h_linkman'      => $_POST['h_linkman'],

			 	 'h_linkman_tel'	     => $_POST['h_linkman_tel'],
			    'h_note'      => $_POST['h_note'],
				 'h_required_service_date'      => $_POST['h_required_service_date'],


			  'h_finished'	     => strtotime($_POST['h_finished']),
			    'h_finished_date'      => $_POST['h_finished_date'],


			);

			/* 保存 */
			$rows = $this->_haul_mod->edit($id, $data);
			if ($this->_haul_mod->has_error())
			{
				$this->show_warning($this->_haul_mod->get_error());
				return;
			}

			$this->show_message('edit_ok',
                '返回运程',     'index.php?app=haul&amp;act=edit&amp;id=' . $id,
                '返回列表',   'index.php?app=haul&amp;act=index'
                );
		}



	}
	function view(){
		$db=&db();
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
		$typelist=array();
		$temp=$db->getAll($sql);

		foreach ($temp as $v){
			$typelist[$v['d_key']]=$v['d_value'];
		}
		$this->assign('typelist', $typelist);

		$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
		$typelist=array();
		$temp=$db->getAll($sql);

		foreach ($temp as $v){
			$typelist[$v['d_key']]=$v['d_value'];
		}
		$this->assign('ctype', $typelist);

		$sql="SELECT * from zys_haul where h_tid=".$id." ORDER BY h_order";
		$haul=$db->getAll($sql);

		$haullist=array();
		if($haul){
			foreach ($haul as $v){
				$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
				$collect=$db->getAll($sql);
				if($collect){
					foreach ($collect as $vv){
						$arrs=array();
						if($vv['h_collect_val']){
							$vv['h_collect_val']=trim($vv['h_collect_val'],',');
							$arrs=explode(',', $vv['h_collect_val']);
						}
						$vv['ar']=$arrs;
						$v['item'][]=$vv;
					}
				}

				$haullist[]=$v;
			}
		}




		$this->assign('haullist', $haullist);
		$this->display('haul.viewcopy.html');
	}

	//删除
	function drop()
	{
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		if($arrs!=null&&count($arrs)>0){
			foreach ($arrs as $v){
				$sql="SELECT h_finished FROM zys_haul WHERE h_tid=".$v;
				$status=$db->getOne($sql);
				if($status==0){
					if (!$this->_haul_mod->drop($v))
					{
						$this->show_warning($this->_haul_mod->get_error());
						return;
					}
				}
			}
		}else{
			$sql="SELECT h_finished FROM zys_haul WHERE h_tid=".$arr;
			$status=$db->getOne($sql);
			if($status==0){
				if (!$this->_haul_mod->drop($arr))
				{
					$this->show_warning($this->_haul_mod->get_error());
					return;
				}
			}
		}
		$this->show_message('删除成功');
	}








	//删除采集信息
	function drop_haul_define_collect()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_haul_collect_mod->drop($arrs))
		{
			$this->show_warning($this->_haul_collect_mod->get_error());

			return;
		}
		$this->show_message('删除成功');
			
	}



















}
